package work.timejay.shop.service.user.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.SM2;
import cn.hutool.crypto.digest.SM3;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import work.timejay.dto.shop.UserLoginDTO;
import work.timejay.dto.shop.UserRegisterDTO;
import work.timejay.enums.ErrorMsg;
import work.timejay.exception.TimeJayException;
import work.timejay.shop.entity.user.User;
import work.timejay.shop.mapper.user.ShopUserMapper;
import work.timejay.shop.service.user.UserService;
import work.timejay.common.SuperServiceImpl;
import org.springframework.stereotype.Service;
import work.timejay.utils.LoginHelper;
import work.timejay.vo.shop.UserLoginVO;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 王文杰
 * @since 2024-01-29
 */
@Service
public class UserServiceImpl extends SuperServiceImpl<ShopUserMapper, User> implements UserService {

    private static final String publicKey = "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAE22qcWgur32jO9mngmJDnho7Yafh4Dee3GCuaGe1q2fSIZjpjix+SsUZpGRnyBVE15xpK681C0aAboIFEw7br7g==";
    private static final String privateKey = "MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQg3e18cLi4w+FLBifQUwFsQMBDrTz2s5fGOT9UZixhoEWgCgYIKoEcz1UBgi2hRANCAATbapxaC6vfaM72aeCYkOeGjthp+HgN57cYK5oZ7WrZ9IhmOmOLH5KxRmkZGfIFUTXnGkrrzULRoBuggUTDtuvu";
    @Override
    public UserLoginVO login(UserLoginDTO dto) {
        User user = lambdaQuery()
                .eq(User::getPhone, dto.getPhone())
                .one();
        if(user == null){
            throw  TimeJayException.fail(ErrorMsg.LOGIN_DATA_IS_NULL);
           // throw TimeJayException.fail("500","123");
        }
        String newPass = SmUtil.sm3().digestHex(dto.getPassword() + user.getSalt());
//        SM2 sm2 = new SM2(privateKey, publicKey);
//        String newPass = sm2.encryptHex(dto.getPassword()+user.getSalt(), KeyType.PublicKey);
        if(!newPass.equals(user.getPassword())){
            throw  TimeJayException.fail(ErrorMsg.LOGIN_PASSWORD_ERROR);
        }
        LoginHelper.loginByJson(user.getId(), JSONUtil.parseObj(user));
        StpUtil.login(user.getId());
        String tokenValue = StpUtil.getTokenValue();
        return new UserLoginVO(tokenValue);
    }

    @Override
    public UserLoginVO register(UserRegisterDTO dto) {
        Integer count = lambdaQuery()
                .eq(User::getPhone, dto.getPhone())
                .count();
        if(count > 0){
            throw TimeJayException.fail(ErrorMsg.REGISTER_USER_IS_EXISTS);
        }
        String salt = RandomUtil.randomString(6);
        //加密
        //String pass = SmUtil.sm2(privateKey, publicKey).encryptHex(dto.getPassword() + salt, KeyType.PublicKey);
        String pass = SmUtil.sm3().digestHex(dto.getPassword() + salt);
        User user = new User();
        user.setName(RandomUtil.randomString(6));
        user.setPassword(pass);
        user.setRoleId(0L);
        user.setSalt(salt);
        user.setPhone(dto.getPhone());
        user.setUserLogo("1");
        user.setId(IdWorker.getId());
        save(user);
        LoginHelper.loginByJson(user.getId(),JSONUtil.parseObj(user));
        StpUtil.login(user.getId());
        return new UserLoginVO(StpUtil.getTokenValue());
    }
}
